<template>
  <el-dialog
    :title="type == 1 ? '添加教师' : '编辑教师'"
    :visible="open"
    width="600px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @close="cancel()"
  >
    <el-form @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="教师姓名" prop="name">
        <el-input
          v-input-restrict
          v-model="form.name"
          maxlength="10"
          show-word-limit
          placeholder="请输入教师姓名"
        />
      </el-form-item>
      <el-form-item label="工号" prop="teacherNum">
        <el-input
          v-input-restrict
          maxlength="20"
          show-word-limit
          v-model="form.teacherNum"
          :disabled="type == 2"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio :label="0">男</el-radio>
          <el-radio :label="1">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNumber">
        <el-input
          v-input-restrict v-model="form.phoneNumber" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item label="身份证号">
        <el-input
          v-input-restrict v-model="form.idCard" placeholder="请输入身份证号" />
      </el-form-item>
      <el-form-item
        :label="`关联${$branchName ? $branchName : '分院'}`"
        prop="branchId"
      >
        <el-select v-model="form.branchId" placeholder="请选择" clearable>
          <el-option
            v-for="(item, index) in branchs"
            :key="index"
            :label="item.branchName"
            :value="item.branchId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-preventReClick @click="cancel">取 消</el-button>
      <el-button v-preventReClick type="primary" @click="submitForm"
        >确 定</el-button
      >

    </div>
  </el-dialog>
</template>

<script>
import { listPost_branch } from "@/api/schoolOrganization/organization";

import { checkidCard, checkPhone } from "@/utils/validate";
import {
  getPost_teacher,
  addPost_teacher,
  updatePost_teacher,
} from "@/api/schoolOrganization/teacher";
export default {
  components: {},
  props: {
    open: Boolean,
    type: Number,
    id: Number,
  },
  data() {
    return {
      // 表单参数
      form: {
        schoolId: undefined,
        branchId: undefined,
        teacherNum: undefined,
        name: undefined,
        sex: undefined,
        phoneNumber: undefined,
        idCard: undefined,
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        branchId: [{ required: true, message: `请选择关联${this.$branchName ? this.$branchName : '分院'}`, trigger: "blur" }],
        teacherNum: [
          { required: true, message: "工号不能为空", trigger: "blur" },
          // {min: 7,message: "工号长度不能小于7位" }
        ],
        sex: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        phoneNumber: [
          { required: true, validator: checkPhone, trigger: "blur" },
        ],
      },
      //   分院列表
      branchs: [],
    };
  },
  watch: {
    open(e) {
      if (e) {
        this.reset();
        this.getBranch();
        if (this.type == 2) {
          getPost_teacher(this.id).then((response) => {
            this.form = response.data;
          });
        }
      }
    },
  },
  methods: {
    getBranch() {
      listPost_branch({
        pageNum: 1,
        pageSize: 9999,
        schoolId: this.$store.getters.schoolId,
      }).then((response) => {
        this.branchs = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.$emit("update:open", false);
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        branchId: undefined,
        teacherNum: undefined,
        name: undefined,
        sex: undefined,
        phoneNumber: undefined,
        idCard: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.schoolId = this.$store.getters.schoolId;
          if (this.form.teacherId != undefined) {
            if (!this.form.branchId){
              this.form.branchName=''
            }
            updatePost_teacher(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.$emit("update:open", false);
              this.$emit("refresh", true);
            });
          } else {
            addPost_teacher(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.$emit("update:open", false);
              this.$emit("refresh", true);
            });
          }
        }
      });
    },
  },
  created() {},
  mounted() {},
};
</script>
<style scoped lang='scss'>
</style>
